﻿using Data;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using UnityEngine;
public enum ParseEnum
{
    ParseXml,
    ParseJson,
    Binary,
}
public interface IDataProcess
{
    T DeserializeData<T>(Stream stream);
    bool SerializeData<T>(Stream stream,T data);
}
public class XmlProcess : IDataProcess
{
    public T DeserializeData<T>(Stream stream)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(T));
        var obj = (T)serializer.Deserialize(stream);
        return obj;
    }

    public bool SerializeData<T>(Stream stream,T data)
    {
        XmlWriterSettings xms = new XmlWriterSettings();
        xms.Indent = true;
        xms.Encoding = System.Text.Encoding.UTF8;
        try
        {
            XmlWriter xmlWriter = XmlWriter.Create(stream, xms);
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            serializer.Serialize(xmlWriter, data);
            return true;
        }
        catch (Exception e)
        {
            Debug.LogError(e.Message);
            return false;
        }  
    }
}
public class BinaryProcess : IDataProcess
{
    public T DeserializeData<T>(Stream stream)
    {
        throw new NotImplementedException();
    }

    public bool SerializeData<T>(Stream stream, T data)
    {
        throw new NotImplementedException();
    }
}
public class JsonProcess : IDataProcess
{
    public T DeserializeData<T>(Stream stream)
    {
        throw new NotImplementedException();
    }

    public bool SerializeData<T>(Stream stream, T data)
    {
        throw new NotImplementedException();
    }
}
public class DataProcessTool
{
    private static IDataProcess GetProcess(ParseEnum parseType)
    {
        IDataProcess process=null;
        switch (parseType)
        {
            case ParseEnum.ParseXml:
                process =new XmlProcess();
                break;
            case ParseEnum.ParseJson:
                process = new JsonProcess();
                break;
            default:
                break;
        }
        return process;
    }

    public static T DeserializeDataAsset<T>(byte[] memery, ParseEnum parseType)
    {
        var process = GetProcess(parseType);
        Stream stream = new MemoryStream(memery);  
        T obj = process.DeserializeData<T>(stream);
        stream.Close();
        return obj;
    }
    public static T DeserializeData<T>(string filePath , ParseEnum parseType)
    {
        var process = GetProcess(parseType);
        Stream stream;
        if (File.Exists(filePath))
        {
             stream = File.Open(filePath, FileMode.Open);
             T obj= process.DeserializeData<T>(stream);
             stream.Close();
             return obj;
        }
        return default(T) ;
    }

    public static bool SerializeData<T>(string filePath, T data, ParseEnum parseType)
    {
        using (Stream stream = File.Open(filePath, FileMode.Create))
        {
            var process = GetProcess(parseType);
            return process.SerializeData<T>(stream, data);
        }
    }
}
